home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2006 May / PCWMAY06.iso / Software / Toolkit / Songbird 0.1 / Songbird_0_1_0.exe / components / sbIMediaLibrary.js < prev    next >
Text File  |  2006-02-07  |  24KB  |  709 lines

  1. /*
  2.  //
  3. // BEGIN SONGBIRD GPL
  4. // 
  5. // This file is part of the Songbird web player.
  6. //
  7. // Copyright⌐ 2006 Pioneers of the Inevitable LLC
  8. // http://songbirdnest.com
  9. // 
  10. // This file may be licensed under the terms of of the
  11. // GNU General Public License Version 2 (the ôGPLö).
  12. // 
  13. // Software distributed under the License is distributed 
  14. // on an ôAS ISö basis, WITHOUT WARRANTY OF ANY KIND, either 
  15. // express or implied. See the GPL for the specific language 
  16. // governing rights and limitations.
  17. //
  18. // You should have received a copy of the GPL along with this 
  19. // program. If not, go to http://www.gnu.org/licenses/gpl.html
  20. // or write to the Free Software Foundation, Inc., 
  21. // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  22. // 
  23. // END SONGBIRD GPL
  24. //
  25.  */
  26.  
  27. //
  28. // sbIMediaLibrary Object
  29. //
  30.  
  31.  
  32. const SONGBIRD_MEDIALIBRARY_CONTRACTID = "@songbird.org/Songbird/MediaLibrary;1";
  33. const SONGBIRD_MEDIALIBRARY_CLASSNAME = "Songbird Media Library Interface";
  34. const SONGBIRD_MEDIALIBRARY_CID = Components.ID("{ca5c9b2c-e061-4c98-8333-9c839efc0d7f}");
  35. const SONGBIRD_MEDIALIBRARY_IID = Components.interfaces.sbIMediaLibrary;
  36.  
  37. const LIBRARY_TABLE_NAME = "library";
  38. const LIBRARY_DESC_TABLE_NAME = "library_desc";
  39.  
  40. const LIBRARY_DESC_TABLE_CREATE = "CREATE TABLE library_desc (column_name TEXT UNIQUE, readable_name TEXT, is_visible INTEGER(0, 1) DEFAULT 0, default_visibility INTEGER(0, 1) DEFAULT 0, is_metadata INTEGER(0, 1) DEFAULT 0, sort_weight INTEGER DEFAULT 0, width INTEGER DEFAULT -1)";
  41. const LIBRARY_TABLE_CREATE = "CREATE TABLE library (id INTEGER PRIMARY KEY, uuid BLOB UNIQUE NOT NULL, service_uuid BLOB NOT NULL, url TEXT UNIQUE DEFAULT '', content_type TEXT DEFAULT '', length TEXT DEFAULT '0', artist TEXT DEFAULT '', title TEXT DEFAULT '', album TEXT DEFAULT '', genre TEXT DEFAULT '', composer TEXT DEFAULT '', producer TEXT DEFAULT '', rating INTEGER DEFAULT 0, track_no INTEGER DEFAULT 0, track_total INTEGER DEFAULT 0, disc_no INTEGER DEFAULT 0, disc_total INTEGER DEFAULT 0, year INTEGER DEFAULT 0)";
  42. const LIBRARY_TABLE_CREATE_INDEX = "CREATE index library_index ON library(id, uuid, url, content_type, length, artist, album, genre)";
  43.  
  44.  
  45. function CMediaLibrary()
  46. {
  47.   try
  48.   {
  49.     jsLoader = Components.classes["@mozilla.org/moz/jssubscript-loader;1"].getService(Components.interfaces.mozIJSSubScriptLoader);
  50.     jsLoader.loadSubScript( "chrome://rmp_demo/content/songbird_interfaces.js", this );
  51.   }
  52.   catch ( err )
  53.   {
  54.     throw "\n\n CMediaLibrary Constructor \r\n" + err;
  55.   }
  56. }
  57.  
  58. /* I actually need a constructor in this case. */
  59. CMediaLibrary.prototype.constructor = CMediaLibrary;
  60.  
  61. /* the CMediaLibrary class def */
  62. CMediaLibrary.prototype = 
  63. {
  64.   m_queryObject: null,
  65.   
  66.   SetQueryObject: function(queryObj)
  67.   {
  68.     this.m_queryObject = queryObj; 
  69.     return;
  70.   },
  71.   
  72.   GetQueryObject: function()
  73.   {
  74.     return this.m_queryObject;
  75.   },
  76.  
  77.   CreateDefaultLibrary: function()
  78.   {
  79.     if(this.m_queryObject != null)
  80.     {
  81.       this.m_queryObject.ResetQuery();
  82.       
  83.       //Create the Library Description Table.
  84.       this.m_queryObject.AddQuery(LIBRARY_DESC_TABLE_CREATE);
  85.       
  86.       //Create the Library Table.
  87.       this.m_queryObject.AddQuery(LIBRARY_TABLE_CREATE);
  88.       //Create the Library Index.
  89.       this.m_queryObject.AddQuery(LIBRARY_TABLE_CREATE_INDEX);
  90.  
  91.       // Strings beginning with & are translated in the UI.
  92.       var id = ( "&metadata.id" );
  93.       var row_id = ( "&metadata.row_id" );
  94.       var uuid = ( "&metadata.uuid" );
  95.       var service_uuid = ( "&metadata.service_uuid" );
  96.       var url = ( "&metadata.url" );
  97.       var content_type = ( "&metadata.content_type" );
  98.       var length = ( "&metadata.length" );
  99.       var artist = ( "&metadata.artist" );
  100.       var title = ( "&metadata.title" );
  101.       var album = ( "&metadata.album" );
  102.       var genre = ( "&metadata.genre" );
  103.       var composer = ( "&metadata.composer" );
  104.       var producer = ( "&metadata.producer" );
  105.       var rating = ( "&metadata.rating" );
  106.       var track_no = ( "&metadata.track_no" );
  107.       var track_total = ( "&metadata.track_total" );
  108.       var disc_no = ( "&metadata.disc_no" );
  109.       var disc_total = ( "&metadata.disc_total" );
  110.       var year = ( "&metadata.year" );
  111.       
  112.       //Fill the Library Description Table.
  113.       this.m_queryObject.AddQuery("INSERT OR REPLACE INTO " + LIBRARY_DESC_TABLE_NAME + " VALUES (\"id\", \"" + id + "\", 1, 0, 0, 0, -1)");
  114.       this.m_queryObject.AddQuery("INSERT OR REPLACE INTO " + LIBRARY_DESC_TABLE_NAME + " VALUES (\"row_id\", \"" + row_id + "\", 1, 1, 0, -10000, 4)");
  115.       this.m_queryObject.AddQuery("INSERT OR REPLACE INTO " + LIBRARY_DESC_TABLE_NAME + " VALUES (\"uuid\", \"" + uuid + "\", 1, 0, 0, 0, -1)");
  116.       this.m_queryObject.AddQuery("INSERT OR REPLACE INTO " + LIBRARY_DESC_TABLE_NAME + " VALUES (\"service_uuid\", \"" + service_uuid + "\", 1, 0, 0, 0, -1)");
  117.       this.m_queryObject.AddQuery("INSERT OR REPLACE INTO " + LIBRARY_DESC_TABLE_NAME + " VALUES (\"url\", \"" + url + "\", 1, 0, 1, 0, -1)");
  118.       this.m_queryObject.AddQuery("INSERT OR REPLACE INTO " + LIBRARY_DESC_TABLE_NAME + " VALUES (\"content_type\", \"" + content_type + "\", 1, 0, 1, 0, -1)");
  119.       this.m_queryObject.AddQuery("INSERT OR REPLACE INTO " + LIBRARY_DESC_TABLE_NAME + " VALUES (\"length\", \"" + length + "\", 1, 1, 1, -8000, 4)");
  120.       this.m_queryObject.AddQuery("INSERT OR REPLACE INTO " + LIBRARY_DESC_TABLE_NAME + " VALUES (\"artist\", \"" + artist + "\", 1, 1, 1, -7000, 25)");
  121.       this.m_queryObject.AddQuery("INSERT OR REPLACE INTO " + LIBRARY_DESC_TABLE_NAME + " VALUES (\"title\", \"" + title + "\", 1, 1, 1, -9000, 60)");
  122.       this.m_queryObject.AddQuery("INSERT OR REPLACE INTO " + LIBRARY_DESC_TABLE_NAME + " VALUES (\"album\", \"" + album + "\", 1, 1, 1, -6000, 25)");
  123.       this.m_queryObject.AddQuery("INSERT OR REPLACE INTO " + LIBRARY_DESC_TABLE_NAME + " VALUES (\"genre\", \"" + genre + "\", 1, 1, 1, -5000, 10)");
  124.       this.m_queryObject.AddQuery("INSERT OR REPLACE INTO " + LIBRARY_DESC_TABLE_NAME + " VALUES (\"composer\", \"" + composer + "\", 1, 0, 1, 0, -1)");
  125.       this.m_queryObject.AddQuery("INSERT OR REPLACE INTO " + LIBRARY_DESC_TABLE_NAME + " VALUES (\"producer\", \"" + producer + "\", 1, 0, 1, 0, -1)");
  126.       this.m_queryObject.AddQuery("INSERT OR REPLACE INTO " + LIBRARY_DESC_TABLE_NAME + " VALUES (\"rating\", \"" + rating + "\", 1, 0, 1, 0, -1)");
  127.       this.m_queryObject.AddQuery("INSERT OR REPLACE INTO " + LIBRARY_DESC_TABLE_NAME + " VALUES (\"track_no\", \"" + track_no + "\", 1, 0, 1, 0, -1)");
  128.       this.m_queryObject.AddQuery("INSERT OR REPLACE INTO " + LIBRARY_DESC_TABLE_NAME + " VALUES (\"track_total\", \"" + track_total + "\", 1, 0, 1, 0, -1)");
  129.       this.m_queryObject.AddQuery("INSERT OR REPLACE INTO " + LIBRARY_DESC_TABLE_NAME + " VALUES (\"disc_no\", \"" + disc_no + "\", 1, 0, 1, 0, -1)");
  130.       this.m_queryObject.AddQuery("INSERT OR REPLACE INTO " + LIBRARY_DESC_TABLE_NAME + " VALUES (\"disc_total\", \"" + disc_total + "\", 1, 0, 1, 0, -1)");
  131.       this.m_queryObject.AddQuery("INSERT OR REPLACE INTO " + LIBRARY_DESC_TABLE_NAME + " VALUES (\"year\", \"" + year + "\", 1, 0, 1, 0, -1)");
  132.       
  133.       this.m_queryObject.Execute();
  134.       this.m_queryObject.WaitForCompletion();
  135.     }
  136.     
  137.     return;
  138.   },
  139.  
  140.   AddMedia: function(strMediaURL, nMetaKeyCount, aMetaKeys, nMetadataValueCount, aMetadataValues, bCheckForUniqueFileName, bWillRunLater)
  141.   {
  142.     if(this.m_queryObject != null)
  143.     {
  144.       var aUUIDGenerator = Components.classes["@mozilla.org/uuid-generator;1"].createInstance(Components.interfaces.nsIUUIDGenerator);
  145.       var guid = aUUIDGenerator.generateUUID();
  146.       var dbguid = this.m_queryObject.GetDatabaseGUID();
  147.       var aDBQuery = Components.classes["@songbird.org/Songbird/DatabaseQuery;1"].createInstance(Components.interfaces.sbIDatabaseQuery);
  148.       var strQuery = "SELECT uuid FROM library WHERE ";
  149.       
  150.       if(bCheckForUniqueFileName)
  151.       {
  152.         var filenamePos = strMediaURL.lastIndexOf("/") + 1;
  153.         if(!filenamePos) filenamePos = strMediaURL.lastIndexOf("\\") + 1;
  154.         
  155.         if(filenamePos)        
  156.         {
  157.           var filename = strMediaURL.substring(filenamePos);
  158.           if(filename != "")
  159.             strQuery += "url LIKE \"%" + filename + "\"";
  160.         }
  161.       }
  162.       else
  163.       {
  164.         strQuery += "url = \"" + strMediaURL + "\"";
  165.       }
  166.       
  167.       aDBQuery.SetAsyncQuery(true);
  168.       aDBQuery.SetDatabaseGUID(dbguid);
  169.       
  170.       aDBQuery.ResetQuery();
  171.       aDBQuery.AddQuery(strQuery);
  172.       
  173.       aDBQuery.Execute();
  174.       aDBQuery.WaitForCompletion();
  175.  
  176.       var resObj = aDBQuery.GetResultObject();
  177.       if(resObj.GetRowCount() > 0)
  178.       {
  179.         guid = resObj.GetRowCell(0, 0);
  180.         if(guid)
  181.         {
  182.           //strQuery = "UPDATE " + LIBRARY_TABLE_NAME + " SET url = \"" + strMediaURL + "\" WHERE uuid = \"" + guid + "\"";
  183.           return guid;
  184.         }
  185.       }
  186.       else
  187.       {
  188.   
  189.         var i = 0;
  190.         strQuery = "INSERT OR REPLACE INTO " + LIBRARY_TABLE_NAME + " (uuid, service_uuid, url";
  191.         for(; i < nMetaKeyCount; i++)
  192.         {
  193.           strQuery += ", ";
  194.           strQuery += aMetaKeys[i];
  195.         }
  196.         
  197.         strQuery += ") VALUES (\"" + guid + "\", \"" + dbguid + "\", \"" + strMediaURL + "\"";
  198.         
  199.         for(i = 0; i < nMetadataValueCount; i++)
  200.         {
  201.           strQuery += ", ";
  202.           aMetadataValues[i] = aMetadataValues[i].replace(/"/g, "\"\"");
  203.           strQuery += "\"" + aMetadataValues[i] + "\"";
  204.         }
  205.         
  206.         strQuery += ")";
  207.       }
  208.         
  209.       if(!bWillRunLater)
  210.         this.m_queryObject.ResetQuery();
  211.       
  212.       this.m_queryObject.AddQuery(strQuery);
  213.       
  214.       if(!bWillRunLater)
  215.       {
  216.         this.m_queryObject.Execute();
  217.         this.m_queryObject.WaitForCompletion();
  218.       }
  219.      
  220.       return guid;
  221.     }
  222.     
  223.     return "";
  224.   },
  225.   
  226.   RemoveMediaByURL: function(strMediaURL, bWillRunLater)
  227.   {
  228.     if(this.m_queryObject != null)
  229.     {
  230.       if(!bWillRunLater)
  231.         this.m_queryObject.ResetQuery();
  232.     
  233.       this.m_queryObject.AddQuery("DELETE FROM " + LIBRARY_TABLE_NAME + " WHERE url = \"" + strMediaURL + "\"");
  234.       
  235.       if(!bWillRunLater)
  236.       {
  237.         this.m_queryObject.Execute();
  238.         this.m_queryObject.WaitForCompletion();
  239.       }
  240.       
  241.       return true;
  242.     }
  243.     
  244.     return false;
  245.   },
  246.   
  247.   RemoveMediaByGUID: function(mediaGUID, bWillRunLater)
  248.   {
  249.     if(this.m_queryObject != null)
  250.     {
  251.       if(!bWillRunLater)
  252.         this.m_queryObject.ResetQuery();
  253.         
  254.       this.m_queryObject.AddQuery("DELETE FROM " + LIBRARY_TABLE_NAME + " WHERE uuid = \"" + mediaGUID + "\"");
  255.       
  256.       const PlaylistManager = new Components.Constructor("@songbird.org/Songbird/PlaylistManager;1", "sbIPlaylistManager");
  257.       var aPlaylistManager = (new PlaylistManager()).QueryInterface(Components.interfaces.sbIPlaylistManager);
  258.       aPlaylistManager.PurgeTrackByGUIDFromPlaylists(mediaGUID, this.m_queryObject.GetDatabaseGUID());
  259.       
  260.       if(!bWillRunLater)
  261.       {
  262.         this.m_queryObject.Execute();
  263.         this.m_queryObject.WaitForCompletion();
  264.       }
  265.       
  266.       return true;
  267.     }
  268.     
  269.     return false;
  270.   },
  271.   
  272.   PurgeDefaultLibrary : function( bWillRunLater)
  273.   {
  274.     if(this.m_queryObject != null)
  275.     {
  276.       if(!bWillRunLater)
  277.         this.m_queryObject.ResetQuery();
  278.         
  279.       this.m_queryObject.AddQuery("DELETE FROM " + LIBRARY_TABLE_NAME );
  280.       
  281.       if(!bWillRunLater)
  282.       {
  283.         this.m_queryObject.Execute();
  284.         this.m_queryObject.WaitForCompletion();
  285.       }
  286.       
  287.       return true;
  288.     }
  289.     
  290.     return false;
  291.   },
  292.   
  293.   FindByGUID: function(mediaGUID)
  294.   {
  295.     var aDBQuery = Components.classes["@songbird.org/Songbird/DatabaseQuery;1"].createInstance(Components.interfaces.sbIDatabaseQuery);
  296.     aDBQuery.SetDatabaseGUID(this.m_queryObject.GetDatabaseGUID());
  297.  
  298.     aDBQuery.AddQuery("SELECT url FROM " + LIBRARY_TABLE_NAME + " WHERE uuid = \"" + mediaGUID + "\"");
  299.       
  300.     aDBQuery.Execute();
  301.     aDBQuery.WaitForCompletion();
  302.       
  303.     var resObj = aDBQuery.GetResultObject();
  304.     return resObj.GetRowCell(0, 0);
  305.   },
  306.   
  307.   FindByURL: function(strURL)
  308.   {
  309.     var aDBQuery = Components.classes["@songbird.org/Songbird/DatabaseQuery;1"].createInstance(Components.interfaces.sbIDatabaseQuery);
  310.     aDBQuery.SetDatabaseGUID(this.m_queryObject.GetDatabaseGUID());
  311.  
  312.     aDBQuery.AddQuery("SELECT uuid FROM " + LIBRARY_TABLE_NAME + " WHERE url = \"" + strURL + "\"");
  313.       
  314.     aDBQuery.Execute();
  315.     aDBQuery.WaitForCompletion();
  316.       
  317.     var resObj = aDBQuery.GetResultObject();
  318.     return resObj.GetRowCell(0, 0);
  319.   },
  320.  
  321.   GetColumnInfo: function()
  322.   {
  323.     if(this.m_queryObject != null)
  324.     {
  325.       this.m_queryObject.ResetQuery();
  326.       this.m_queryObject.AddQuery("SELECT * FROM " + LIBRARY_DESC_TABLE_NAME + " ORDER BY sort_weight, column_name ASC");
  327.       
  328.       this.m_queryObject.Execute();
  329.       this.m_queryObject.WaitForCompletion();
  330.     }
  331.   },
  332.   
  333.   SetColumnInfo: function(strColumn, strReadableName, isVisible, defaultVisibility, isMetadata, sortWeight, colWidth, bWillRunLater)
  334.   {
  335.     if(this.m_queryObject != null)
  336.     {
  337.       if(!bWillRunLater)
  338.         this.m_queryObject.ResetQuery();
  339.       
  340.       var strQuery = "UPDATE " + LIBRARY_DESC_TABLE_NAME + " SET ";
  341.       strQuery += "readable_name = \"" + strReadableName + "\"";
  342.       strQuery += "is_visible = \"" + isVisible ? 1: 0 + "\"";
  343.       strQuery += "default_visibility = \"" + defaultVisibility ? 1 : 0 + "\"";
  344.       strQuery += "is_metadata = \"" + isMetadata ? 1 : 0 + "\"";
  345.       strQuery += "sort_weight = \"" + sortWeight + "\"";
  346.       strQuery += "width = \"" + colWidth + "\"";
  347.       strQuery += " WHERE column_name = \"" + strColumn + "\"";
  348.       
  349.       this.m_queryObject.AddQuery(strQuery);
  350.       
  351.       if(!bWillRunLater)
  352.       {
  353.         this.m_queryObject.Execute();
  354.         this.m_queryObject.WaitForCompletion();
  355.       }
  356.     }    
  357.   },
  358.  
  359.   GetMetadataFields: function(nMetadataFieldCount)
  360.   {
  361.     var aMetadataFields = new Array();
  362.     nMetadataFieldCount.value = 0;
  363.     
  364.     if(this.m_queryObject != null)
  365.     {
  366.       this.m_queryObject.ResetQuery();
  367.       this.m_queryObject.AddQuery("SELECT column_name FROM " + LIBRARY_DESC_TABLE_NAME + " WHERE is_metadata = 1");
  368.       
  369.       this.m_queryObject.Execute();
  370.       this.m_queryObject.WaitForCompletion();
  371.  
  372.       var resObj = this.m_queryObject.GetResultObject();
  373.       nMetadataFieldCount.value = resObj.GetRowCount();
  374.       
  375.       for(var i = 0; i < nMetadataFieldCount.value; i++)
  376.       {
  377.         aMetadataFields.push(resObj.GetRowCell(i, 0));
  378.       }
  379.     }
  380.     
  381.     return aMetadataFields;
  382.   },
  383.   
  384.   AddMetadataField: function(strField, bWillRunLater)
  385.   {
  386.     if(this.m_queryObject != null)
  387.     {
  388.       if(bWillRunLater)
  389.       {
  390.       }
  391.     }
  392.     
  393.     return;
  394.   },
  395.   
  396.   DeleteMetadataField: function(strField, bWillRunLater)
  397.   {
  398.     if(this.m_queryObject != null)
  399.     {
  400.       if(bWillRunLater)
  401.       {
  402.       }
  403.     }
  404.     
  405.     return;
  406.   },
  407.   
  408.   SetValueByIndex: function(mediaIndex, strField, strValue, bWillRunLater)
  409.   {
  410.     if(this.m_queryObject != null)
  411.     {
  412.       if(!bWillRunLater)
  413.         this.m_queryObject.ResetQuery();
  414.       
  415.       strValue = strValue.replace(/"/g, "\"\"");
  416.       this.m_queryObject.AddQuery("UPDATE " + LIBRARY_TABLE_NAME + " SET " + strField + " = \"" + strValue + "\" WHERE id = \"" + mediaIndex + "\"");
  417.       
  418.       if(!bWillRunLater)
  419.       {
  420.         this.m_queryObject.Execute();
  421.         this.m_queryObject.WaitForCompletion();
  422.       }
  423.       
  424.       return true;
  425.     }
  426.     
  427.     return false;
  428.   },
  429.   
  430.   SetValuesByIndex: function(mediaIndex, nMetaKeyCount, aMetaKeys, nMetaValueCount, aMetaValues, bWillRunLater)
  431.   {
  432.     if(this.m_queryObject != null ||
  433.        nMetaKeyCount != nMetaValueCount)
  434.     {
  435.       if(!bWillRunLater)
  436.         this.m_queryObject.ResetQuery();
  437.       
  438.       var strQuery = "UPDATE " + LIBRARY_TABLE_NAME + " SET ";
  439.       var i = 0;
  440.       for(; i < nMetaKeyCount; i++)
  441.       {
  442.         aMetaValues[i] = aMetaValues[i].replace(/"/g, "\"\"");
  443.         strQuery += aMetaKeys[i] + " = \"" + aMetaValues[i] + "\"";
  444.         if(i < nMetaKeyCount - 1)
  445.           strQuery += ", ";
  446.       }
  447.       
  448.       strQuery += " WHERE id = \"" + mediaIndex + "\"";
  449.       this.m_queryObject.AddQuery(strQuery);
  450.       
  451.       if(!bWillRunLater)
  452.       {
  453.         this.m_queryObject.Execute();
  454.         this.m_queryObject.WaitForCompletion();
  455.       }
  456.       
  457.       return true;
  458.     }
  459.     
  460.     return false;
  461.   },
  462.   
  463.   GetValueByIndex: function(mediaIndex, strField)
  464.   {
  465.     if(this.m_queryObject != null)
  466.     {
  467.       this.m_queryObject.ResetQuery();
  468.       this.m_queryObject.AddQuery("SELECT " + strField + " FROM " + LIBRARY_TABLE_NAME + " WHERE id = \"" + mediaIndex + "\"");
  469.       
  470.       this.m_queryObject.Execute();
  471.       this.m_queryObject.WaitForCompletion();
  472.       
  473.       var resObj = this.m_queryObject.GetResultObject();
  474.       
  475.       if(resObj.GetRowCount())
  476.         return resObj.GetRowCell(0, 0);
  477.     }
  478.     
  479.     return "";
  480.   },
  481.   
  482.   GetValuesByIndex: function(mediaIndex, nMetaKeyCount, aMetaKeys, nMetadataValueCount)
  483.   {
  484.     nMetadataValueCount = 0;
  485.     var aMetadataValues = new Array();
  486.     
  487.     if(this.m_queryObject != null)
  488.     {
  489.       var strQuery = "SELECT ";
  490.       this.m_queryObject.ResetQuery();
  491.       
  492.       var i = 0;
  493.       for( ; i < nMetaKeyCount; i++)
  494.       {
  495.         strQuery += aMetaKeys[i];
  496.         
  497.         if(i < nMetaKeyCount - 1)
  498.           strQuery += ", ";
  499.       }
  500.       
  501.       strQuery += " FROM " + LIBRARY_TABLE_NAME + " WHERE id = \"" + mediaIndex + "\"";
  502.       this.m_queryObject.AddQuery(strQuery);
  503.       
  504.       this.m_queryObject.Execute();
  505.       this.m_queryObject.WaitForCompletion();
  506.       
  507.       var resObj = this.m_queryObject.GetResultObject();
  508.       nMetadataValueCount = resObj.GetColumnCount();
  509.       
  510.       for(var i = 0; i < nMetadataValueCount; i++)
  511.       {
  512.         aMetadataValues.push(resObj.GetRowCell(0, i));
  513.       }
  514.     }
  515.     
  516.     return aMetadataValues;
  517.   },
  518.   
  519.   SetValueByGUID: function(mediaGUID, strField, strValue, bWillRunLater)
  520.   {
  521.     if(this.m_queryObject != null)
  522.     {
  523.       if(!bWillRunLater)
  524.         this.m_queryObject.ResetQuery();
  525.       
  526.       strValue = strValue.replace(/"/g, "\"\"");
  527.       this.m_queryObject.AddQuery("UPDATE " + LIBRARY_TABLE_NAME + " SET " + strField + " = \"" + strValue + "\" WHERE uuid = \"" + mediaGUID + "\"");
  528.       
  529.       if(!bWillRunLater)
  530.       {
  531.         this.m_queryObject.Execute();
  532.         this.m_queryObject.WaitForCompletion();
  533.       }
  534.     }
  535.     
  536.     return;
  537.   },
  538.  
  539.   SetValuesByGUID: function(mediaGUID, nMetaKeyCount, aMetaKeys, nMetaValueCount, aMetaValues, bWillRunLater)
  540.   {
  541.     if(this.m_queryObject != null ||
  542.        nMetaKeyCount != nMetaValueCount)
  543.     {
  544.       if(!bWillRunLater)
  545.         this.m_queryObject.ResetQuery();
  546.       
  547.       var strQuery = "UPDATE " + LIBRARY_TABLE_NAME + " SET ";
  548.       var i = 0;
  549.       for(; i < nMetaKeyCount; i++)
  550.       {
  551.         aMetaValues[i] = aMetaValues[i].replace(/"/g, "\"\"");
  552.         strQuery += aMetaKeys[i] + " = \"" + aMetaValues[i] + "\"";
  553.         if(i < nMetaKeyCount - 1)
  554.           strQuery += ", ";
  555.       }
  556.       
  557.       strQuery += " WHERE uuid = \"" + mediaGUID + "\"";
  558.       this.m_queryObject.AddQuery(strQuery);
  559.       
  560.       if(!bWillRunLater)
  561.       {
  562.         this.m_queryObject.Execute();
  563.         this.m_queryObject.WaitForCompletion();
  564.       }
  565.       
  566.       return true;
  567.     }
  568.     
  569.     return false;
  570.   },
  571.   
  572.   GetValueByGUID: function(mediaGUID, strField)
  573.   {
  574.     if(this.m_queryObject != null)
  575.     {
  576.       this.m_queryObject.ResetQuery();
  577.       this.m_queryObject.AddQuery("SELECT " + strField + " FROM " + LIBRARY_TABLE_NAME + " WHERE uuid = \"" + mediaGUID + "\"");
  578.       
  579.       this.m_queryObject.Execute();
  580.       this.m_queryObject.WaitForCompletion();
  581.       
  582.       var resObj = this.m_queryObject.GetResultObject();
  583.       
  584.       if(resObj.GetRowCount())
  585.         return resObj.GetRowCell(0, 0);
  586.     }
  587.     
  588.     return "";
  589.   },
  590.   
  591.   GetValuesByGUID: function(mediaGUID, nMetaKeyCount, aMetaKeys, nMetadataValueCount)
  592.   {
  593.     nMetadataValueCount = 0;
  594.     var aMetadataValues = new Array();
  595.     
  596.     if(this.m_queryObject != null)
  597.     {
  598.       var strQuery = "SELECT ";
  599.       this.m_queryObject.ResetQuery();
  600.       
  601.       var i = 0;
  602.       for( ; i < nMetaKeyCount; i++)
  603.       {
  604.         strQuery += aMetaKeys[i];
  605.         
  606.         if(i < nMetaKeyCount - 1)
  607.           strQuery += ", ";
  608.       }
  609.       
  610.       strQuery += " FROM " + LIBRARY_TABLE_NAME + " WHERE uuid = \"" + mediaGUID + "\"";
  611.       this.m_queryObject.AddQuery(strQuery);
  612.       
  613.       this.m_queryObject.Execute();
  614.       this.m_queryObject.WaitForCompletion();
  615.       
  616.       var resObj = this.m_queryObject.GetResultObject();
  617.       nMetadataValueCount = resObj.GetColumnCount();
  618.       
  619.       for(var i = 0; i < nMetadataValueCount; i++)
  620.       {
  621.         aMetadataValues.push(resObj.GetRowCell(0, i));
  622.       }
  623.     }
  624.     
  625.     return aMetadataValues;
  626.   },
  627.  
  628.   QueryInterface: function(iid)
  629.   {
  630.       if (!iid.equals(Components.interfaces.nsISupports) &&
  631.           !iid.equals(SONGBIRD_MEDIALIBRARY_IID))
  632.           throw Components.results.NS_ERROR_NO_INTERFACE;
  633.       return this;
  634.   }
  635.   
  636. };
  637.  
  638. /**
  639.  * \class sbMediaLibraryModule
  640.  * \brief 
  641.  */
  642. var sbMediaLibraryModule = 
  643. {
  644.   //firstTime: true,
  645.   
  646.   registerSelf: function(compMgr, fileSpec, location, type)
  647.   {
  648.     //if(this.firstTime)
  649.     //{
  650.       //this.firstTime = false;
  651.       //throw Components.results.NS_ERROR_FACTORY_REGISTER_AGAIN;
  652.     //}
  653.  
  654.     compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
  655.     compMgr.registerFactoryLocation(SONGBIRD_MEDIALIBRARY_CID, 
  656.                                     SONGBIRD_MEDIALIBRARY_CLASSNAME, 
  657.                                     SONGBIRD_MEDIALIBRARY_CONTRACTID, 
  658.                                     fileSpec, 
  659.                                     location,
  660.                                     type);
  661.   },
  662.  
  663.   getClassObject: function(compMgr, cid, iid) 
  664.   {
  665.     if(!cid.equals(SONGBIRD_MEDIALIBRARY_CID))
  666.         throw Components.results.NS_ERROR_NO_INTERFACE;
  667.  
  668.     if(!iid.equals(Components.interfaces.nsIFactory))
  669.         throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
  670.  
  671.     return sbMediaLibraryFactory;
  672.   },
  673.  
  674.   canUnload: function(compMgr)
  675.   { 
  676.     return true; 
  677.   }
  678. };
  679.  
  680. /**
  681.  * \class sbMediaLibraryFactory
  682.  * \brief 
  683.  */
  684. var sbMediaLibraryFactory =
  685. {
  686.   createInstance: function(outer, iid)
  687.   {
  688.     if (outer != null)
  689.         throw Components.results.NS_ERROR_NO_AGGREGATION;
  690.  
  691.     if (!iid.equals(SONGBIRD_MEDIALIBRARY_IID) &&
  692.         !iid.equals(Components.interfaces.nsISupports))
  693.         throw Components.results.NS_ERROR_INVALID_ARG;
  694.  
  695.     return (new CMediaLibrary()).QueryInterface(iid);
  696.   }
  697. }; //sbMediaLibraryFactory
  698.  
  699. /**
  700.  * \function NSGetModule
  701.  * \brief 
  702.  */
  703. function NSGetModule(comMgr, fileSpec)
  704. {
  705.  
  706.   return sbMediaLibraryModule;
  707. } //NSGetModule
  708.  
  709.